package com.haixiaoke.business;

import com.haixiaoke.common.core.domain.AjaxResult;
import com.haixiaoke.common.core.page.TableDataInfo;
import com.haixiaoke.common.utils.poi.ExcelUtil;
import com.haixiaoke.domain.SmartLock;
import com.haixiaoke.domain.SmartLockBluetooth;
import com.haixiaoke.service.ISmartLockBluetoothService;
import com.haixiaoke.service.ISmartLockService;
import com.haixiaoke.smartLock.huixiangjia.SmartLockHxjEventHandleService;
import com.haixiaoke.smartLock.tianwanghulian.SmartLockTwhlEventHandleService;
import com.haixiaoke.smartLock.yaluete.SmartLockYLTEventService;
import com.haixiaoke.system.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 蓝牙授权信息Controller
 *
 * @author xiaolong
 * @date 2023-05-27
 */
@RestController
@RequestMapping("/business/bluetooth")
public class SmartLockBluetoothController extends BaseController {
    @Autowired
    private ISmartLockBluetoothService smartLockBluetoothService;
    @Autowired
    private ISmartLockService smartLockService;
    @Autowired
    private SmartLockTwhlEventHandleService smartLockTwhlEventHandleService;
    @Autowired
    private SmartLockYLTEventService smartLockYLTEventService;
    @Autowired
    private SmartLockHxjEventHandleService smartLockHxjEventHandleService;

    /**
     * 查询蓝牙授权信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SmartLockBluetooth smartLockBluetooth) {
        startPage();
        List<SmartLockBluetooth> list = smartLockBluetoothService.selectSmartLockBluetoothList(smartLockBluetooth);
        return getDataTable(list);
    }

    /**
     * 导出蓝牙授权信息列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, SmartLockBluetooth smartLockBluetooth) {
        List<SmartLockBluetooth> list = smartLockBluetoothService.selectSmartLockBluetoothList(smartLockBluetooth);
        ExcelUtil<SmartLockBluetooth> util = new ExcelUtil<SmartLockBluetooth>(SmartLockBluetooth.class);
        util.exportExcel(response, list, "蓝牙授权信息数据");
    }

    /**
     * 获取蓝牙授权信息详细信息
     */
    @GetMapping(value = "/{bluetoothId}")
    public AjaxResult getInfo(@PathVariable("bluetoothId") Long bluetoothId) {
        return success(smartLockBluetoothService.selectSmartLockBluetoothByBluetoothId(bluetoothId));
    }

    /**
     * 新增蓝牙授权信息和授权蓝牙开锁
     */
    @PostMapping
    public AjaxResult add(@RequestBody SmartLockBluetooth smartLockBluetooth) {
        SmartLock smartLock = smartLockService.selectBySn(smartLockBluetooth.getSn());
        if (smartLock.getBrandId() == SmartLock.THIRD_GENERATION_SMART_LOCK) {
            return toAjax(smartLockYLTEventService.insertSmartLockBluetooth(smartLockBluetooth));
        } else if (smartLock.getBrandId() == SmartLock.FOUR_GENERATION_SMART_LOCK) {
            return toAjax(smartLockTwhlEventHandleService.insertSmartLockBluetooth(smartLockBluetooth));
        } else if (smartLock.getBrandId() == SmartLock.SEVEN_GENERATION_SMART_LOCK) {
            return toAjax(smartLockHxjEventHandleService.insertSmartLockBluetooth(smartLockBluetooth));
        } else {
            return AjaxResult.error("该设备没有蓝牙开锁功能");
        }
    }

    /**
     * 修改蓝牙授权信息
     */
    @PutMapping
    public AjaxResult edit(@RequestBody SmartLockBluetooth smartLockBluetooth) {
        return toAjax(smartLockBluetoothService.updateSmartLockBluetooth(smartLockBluetooth));
    }

    /**
     * 删除蓝牙授权信息
     */
    @DeleteMapping("/{bluetoothIds}")
    public AjaxResult remove(@PathVariable Long[] bluetoothIds) {
        return toAjax(smartLockBluetoothService.deleteSmartLockBluetoothByBluetoothIds(bluetoothIds));
    }
}
